package com.rbac.controller;

import com.rbac.annotation.RequiresPermission;
import com.rbac.common.Result;
import com.rbac.dto.PermissionQueryDTO;
import com.rbac.entity.SysPermission;
import com.rbac.service.PermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/permission")
@CrossOrigin
public class PermissionController {
    
    @Autowired
    private PermissionService permissionService;
    
    @GetMapping("/list")
    @RequiresPermission("system:permission:query")
    public Result<PageInfo> getPermissionList(PermissionQueryDTO queryDTO) {
        PageInfo permissionList = permissionService.getPermissionList(queryDTO);
        return Result.success(permissionList);
    }
    
    @GetMapping("/all")
    @RequiresPermission("system:permission:query")
    public Result<List<SysPermission>> getAllPermissions() {
        List<SysPermission> permissions = permissionService.getAllPermissions();
        return Result.success(permissions);
    }
    
    @GetMapping("/{id}")
    @RequiresPermission("system:permission:query")
    public Result<SysPermission> getPermissionById(@PathVariable Long id) {
        SysPermission permission = permissionService.getPermissionById(id);
        if (permission != null) {
            return Result.success(permission);
        } else {
            return Result.error("权限不存在");
        }
    }
    
    @PostMapping
    @RequiresPermission("system:permission:add")
    public Result<String> addPermission(@Valid @RequestBody SysPermission permission) {
        boolean success = permissionService.addPermission(permission);
        if (success) {
            return Result.success("新增权限成功");
        } else {
            return Result.error("新增权限失败，权限编码可能已存在");
        }
    }
    
    @PutMapping
    @RequiresPermission("system:permission:edit")
    public Result<String> updatePermission(@Valid @RequestBody SysPermission permission) {
        boolean success = permissionService.updatePermission(permission);
        if (success) {
            return Result.success("更新权限成功");
        } else {
            return Result.error("更新权限失败");
        }
    }
    
    @DeleteMapping("/{id}")
    @RequiresPermission("system:permission:delete")
    public Result<String> deletePermission(@PathVariable Long id) {
        boolean success = permissionService.deletePermission(id);
        if (success) {
            return Result.success("删除权限成功");
        } else {
            return Result.error("删除权限失败");
        }
    }
    
    @PutMapping("/{id}/status")
    @RequiresPermission("system:permission:edit")
    public Result<String> updatePermissionStatus(@PathVariable Long id, @RequestParam Integer status) {
        boolean success = permissionService.updatePermissionStatus(id, status);
        if (success) {
            return Result.success("更新权限状态成功");
        } else {
            return Result.error("更新权限状态失败");
        }
    }
} 